﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LServer.Model.Test
{
    public class TestRedis
    {
        public static void TestString() 
        {
            //RedisHelper.Set("testName1", "xxl1");
            //string str = RedisHelper.Get("testName1");

            //如果没有testName2 写入并且返回，如果已经存在key了，不写入
            string str = RedisHelper.CacheShell("testName2", -1, () => { return "xxl2"; });

            Console.WriteLine("向redis 中写入一个key的值: " + str);
        }

        public static void TestHash() 
        {
            string key = "RoleHash";
            //RedisHelper.HSet(key,"1", "xxl1");
            //RedisHelper.HSet(key, "2", "xxl2");
            //RedisHelper.HSet(key, "3", "xxl3");
            //string value = RedisHelper.HGet(key, "1");
            //Console.WriteLine("得到一个hash 的值: " + value);

            //返回一个数组
            var ret = RedisHelper.HScan(key, 0);
            Console.WriteLine("得到一个hash 的值 field=: " + ret.Items[0].field);
            Console.WriteLine("得到一个hash 的值 value=: " + ret.Items[0].value);
        }

        public static void TestList() 
        {
            string key = "list";

            //RedisHelper.RPush(key, "元素1", "元素2", "元素3");

            //string str = RedisHelper.LIndex(key, 13);
            //Console.WriteLine("从list中获取一个元素 ：" + str );

            //RedisHelper.LSet(key, 2, "元素3改变");

            //string[] strArr = RedisHelper.LRange(key, 0, 3);
            //foreach (var item in strArr) 
            //{
            //    Console.WriteLine("元素 ：" + item);
            //}

            //RedisHelper.LInsertAfter(key, "元素2", "元素3After");

            string str = RedisHelper.LPop(key);
            Console.WriteLine("从列表中 pop 一个元素 ： " + str);
        }

        public static void TestSet() 
        {
            string key = "nickName";

            //重复插入，如果写入ret=0 就是已经有重复数据了
            //long ret = RedisHelper.SAdd(key, "xxl1", "xxl2", "xxl3","xxl4");
            //Console.WriteLine("插入结果：" + ret);

            //查找元素是否存在
            //bool isExist = RedisHelper.SIsMember(key, "xxl1");
            //Console.WriteLine("是否存在元素：" + isExist);

            //long count = RedisHelper.SCard(key);
            //Console.WriteLine("集合数量：" + count);

            //移除并移除元素
            //string str = RedisHelper.SPop(key);
            //Console.WriteLine("从集合中pop出元素：" + str);

            //移除
            //RedisHelper.SRem(key, "xxl4");
        }

        public static void TestZSet() 
        {
            string key = "rank_fatting";

            //RedisHelper.ZAdd(key, (100, 1), (100, 2), (50, 3), (60, 4), (80, 5));

            //string[] arr = RedisHelper.ZRange(key, 0, 5);
            //foreach (var item in arr)
            //{
            //    Console.WriteLine("有序集合=" + item);
            //}

            //(string member, double score)[] lst = RedisHelper.ZRevRangeWithScores(key, 0, 5);
            //foreach (var item in lst)
            //{
            //    Console.WriteLine("有序集合member=" + item.score);
            //    Console.WriteLine("有序集合member=" + item.member);
            //}

            //根据分数范围 找角色
            //string[] arr = RedisHelper.ZRangeByScore(key, 40, 90);
            //foreach (var item in arr)
            //{
            //    Console.WriteLine("有序集合=" + item);
            //}
        }
    }
}
